import { View, Text } from '@tarojs/components'
import Taro, { useState } from '@tarojs/taro'
import { AtButton, AtCard, AtInput, AtMessage } from 'taro-ui'
import 'taro-ui/dist/style/index.scss'
import servicePath from '../../config/apiUrl'
import { axios } from 'taro-axios'

function Message(props) {


  const testHandle = () => {
    Taro.request({
      url: 'http://127.0.0.1:7001/default/getWebList'
    }).then(res => {
      console.log(res.data)
      setArticleList(res.data.data)
    })
  }

  const [articleList, setArticleList] = useState([])

  const [userName, setUserName] = useState('')
  const [password, setPassword] = useState('')
  const [isLoading, setIsLoading] = useState(false)

  const checkLogin = () => {
    setIsLoading(true)

    if (!userName) {
      Taro.atMessage({
        'message': '用户名不能为空',
        'type': 'error',
      })
      setTimeout(() => {
        setIsLoading(false)
      }, 500)
      return false
    } else if (!password) {
      console.log(userName)
      Taro.atMessage({
        'message': '密码不能为空',
        'type': 'error',
      })
      setTimeout(() => {
        setIsLoading(false)
      }, 500)
      return false
    }
    let dataProps = {
      'userName': userName,
      'password': password
    }
    console.log(userName)
    console.log(password)
    axios({
      method: 'post',
      url: servicePath.checkLogin,
      data: dataProps,
      withCredentials: true
    }).then(
      res => {
        console.log(res.data)
        setIsLoading(false)

        if (res.data.data == '登录成功') {
          //localStorage.setItem('openId',res.data.openId)
          console.log('res.data.userName=' + res.data.userName);
          Taro.setStorage({
            key: 'userName',
            data: res.data.userName
          }).then(res => res)
          Taro.redirectTo({
            url: '/pages/goods/goods'
          })
        } else {
          Taro.atMessage({
            'message': '用户名密码错误',
            'type': 'error',
          })
        }


      })

    setTimeout(() => {
      setIsLoading(false)
    }, 1000)
  }

  return (
    <View className='login-view'>
      <AtCard title="Bobo Test System"  >
        <AtInput
          title='username'
          name="userName"
          placeholder="Enter your userName"
          value={userName}
          onChange={setUserName}
        >
        </AtInput>

        <AtInput
          type='password'
          title='password'
          placeholder="Enter your password"
          value={password}
          onChange={setPassword}
        >
        </AtInput>
        <AtMessage />
        <AtButton
          loading={isLoading}
          disabled={isLoading}
          type='primary'
          size='normal'
          onClick={checkLogin}
        >
          Login in
          </AtButton>
      </AtCard>



      <Text>message page</Text>
      <AtButton type='primary' onClick={testHandle}>获取数据</AtButton>
      {
        articleList.map((item, index) => {
          return (<View key={index}>- {item.Name} </View>)
        })
      }
    </View>
  )

}
export default Message
